package org.example.erat.config;

import org.example.erat.log.core.LoggerSingleton;
import org.example.erat.log.observer.ConsoleLogObserver;
import org.example.erat.log.observer.FileLogObserver;
import org.example.erat.log.service.OperationLogger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志配置类
 * 负责初始化日志系统
 */
@Component
public class LoggingConfig implements ApplicationListener<ApplicationReadyEvent> {
    
    private final FileLogObserver fileLogObserver;
    private final ConsoleLogObserver consoleLogObserver;
    private final OperationLogger operationLogger;
    
    @Value("${spring.application.name:实验报告分析系统}")
    private String applicationName;
    
    @Value("${spring.application.version:1.0.0}")
    private String applicationVersion;
    
    public LoggingConfig(FileLogObserver fileLogObserver, 
                         ConsoleLogObserver consoleLogObserver,
                         OperationLogger operationLogger) {
        this.fileLogObserver = fileLogObserver;
        this.consoleLogObserver = consoleLogObserver;
        this.operationLogger = operationLogger;
    }
    
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 初始化日志单例
        LoggerSingleton logger = LoggerSingleton.getInstance();
        
        // 添加日志观察者
        logger.addObserver(fileLogObserver);
        logger.addObserver(consoleLogObserver);
        
        // 创建日志目录
        createLogDirectories();
        
        // 记录应用启动日志
        logger.info("应用启动完成，初始化日志系统");
        logger.info("支持的设计模式：工厂模式、组合模式、单例模式、策略模式、观察者模式");
        
        // 记录操作日志
        operationLogger.logSystemStartup(applicationName, applicationVersion);
    }
    
    /**
     * 创建日志目录
     */
    private void createLogDirectories() {
        // 创建日志根目录
        File logDir = new File("logs");
        if (!logDir.exists()) {
            boolean created = logDir.mkdirs();
            if (!created) {
                System.err.println("创建日志目录失败: logs");
            }
        }
        
        // 创建按日期分类的日志目录
        String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File todayDir = new File("logs/" + today);
        if (!todayDir.exists()) {
            boolean created = todayDir.mkdirs();
            if (!created) {
                System.err.println("创建日志目录失败: logs/" + today);
            }
        }
    }
    
    /**
     * 应用关闭监听器
     */
    @Component
    public static class ApplicationShutdownListener implements ApplicationListener<ContextClosedEvent> {
        
        private final OperationLogger operationLogger;
        
        @Value("${spring.application.name:实验报告分析系统}")
        private String applicationName;
        
        public ApplicationShutdownListener(OperationLogger operationLogger) {
            this.operationLogger = operationLogger;
        }
        
        @Override
        public void onApplicationEvent(ContextClosedEvent event) {
            // 记录应用关闭日志
            LoggerSingleton.getInstance().info("应用正在关闭");
            operationLogger.logSystemShutdown(applicationName);
        }
    }
}
